package com.example.communist_app_api.service.impl;

import com.example.communist_app_api.db.dao.RoleDao;
import com.example.communist_app_api.db.pojoDto.Role;
import com.example.communist_app_api.service.RoleService;
import com.example.communist_app_api.util.R;
import com.example.communist_app_api.vo.web.req.RoleShowReq;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author love~mlm
 * @version 1.0
 * Create by 2022/11/19 13:32
 */
@Transactional
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public ArrayList<Role> getRoleByRoleId(ArrayList<Integer> roleIdList) {
        return roleDao.getRoleByRoleId(roleIdList);
    }
    //分页及带条件查询
    @Override
    public PageInfo<Role> findPage(RoleShowReq req) {

        Integer pageNum = req.getPageNum();
        Integer pageSize = req.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        if (req.getRoleName() == null) {
            req.setRoleName("");
        }
        List<Role> roleList = roleDao.findPage(req.getRoleName());
        return new PageInfo<Role>(roleList, 5);
    }

    @Override
    public void createNewRole(String roleName) {
        //查询是否存在
        Role role = roleDao.getRoleByRoleName(roleName);
        if (role == null) {
            roleDao.createNewRole(roleName);
        }else {
             R.error("添加角色异常，角色名称重复");
        }

    }

    @Override
    public void deleteRoleByRoleId(Integer roleId) {
        //删除角色
        //查询角色是否存在
        Role role = roleDao.getRoleBySingleRoleId(roleId);
        if (role != null) {
            roleDao.deleteRoleByRoleId(roleId);
        }else {
            R.error("查无该角色，角色ID出错");
        }
    }

    @Override
    public List<Role> getAllRole() {
        return roleDao.getAllRole();
    }

    @Override
    public Integer selectRoleIdByRoleName(String roleName) {
        return roleDao.selectRoleIdByRoleName(roleName);
    }

    @Override
    public String getRoleNameByUserId(Integer userId) {
        return roleDao.getRoleNameByUserId(userId);
    }

    @Override
    public Integer getRoleIdByRoleName(String roleName) {
        return roleDao.getRoleIdByRoleName(roleName);
    }


}
